<?php
/**
 * Search management classes (handles manipulation of saved searches).
 *
 * Copyright 2003 Mark O'Sullivan
 * This file is part of Lussumo's Software Library.
 * Lussumo's Software Library is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
 * Lussumo's Software Library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along with Vanilla; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * The latest source code is available at www.lussumo.com
 * Contact Mark O'Sullivan at mark [at] lussumo [dot] com
 *
 * @author Mark O'Sullivan
 * @copyright 2003 Mark O'Sullivan
 * @license http://lussumo.com/community/gpl.txt GPL 2
 * @package Vanilla
 * @version 1.1.2
 */

/**
 * Search management classes (handles manipulation of saved searches).
 * @package Vanilla
 */
class SearchManager
{
   var $Name = 'SearchManager'; // The name of this class
   var $Context; // The context object that contains all global objects (database, error manager, warning collector, session, etc)

    function __construct()
    {
        $this->Context = Context::getInstance();
    }

    function DeleteSearch($SearchID)
    {
        $s = ObjectFactory::getInstance()->NewObject('SqlBuilder');
        $s->SetMainTable('UserSearch', 'us');
        $s->AddWhere('us', 'SearchID', '', $SearchID, '=');
        $s->AddWhere('us', 'UserID', '', Session::getInstance()->UserID, '=');
        Database::getInstance()->Delete($s, $this->Name, 'DeleteSearch', 'An error occurred while deleting your search.');
        return true;
    }

    // Returns a SqlBuilder object with all of the topic properties already defined in the select
    function GetSearchBuilder()
    {
        $s = ObjectFactory::getInstance()->NewObject('SqlBuilder');
        $s->SetMainTable('UserSearch', 'us');
        $s->AddSelect(array('SearchID', 'Label', 'UserID', 'Type', 'Keywords'), 'us');
        return $s;
    }

    function GetSearchById($SearchID)
    {
        $db = Database::getInstance();
        $Search = ObjectFactory::getInstance()->NewObject('Search');
        $s = $this->GetSearchBuilder();
        $s->AddWhere('us', 'SearchID', '', $SearchID, '=');
        $s->AddWhere('us', 'UserID', '', Session::getInstance()->UserID, '=');
        $result = $db->Select($s, $this->Name, 'GetSearchById', 'An error occurred while attempting to retrieve the requested search.');
        if ($db->RowCount($result) == 0) {
            $this->Context->WarningCollector->Add($this->Context->GetDefinition('ErrSearchNotFound'));
        }
        while ($rows = $db->GetRow($result)) {
            $Search->GetPropertiesFromDataSet($rows, 1);
        }
        return $this->Context->WarningCollector->Iif($Search, false);
    }

    function GetSearchCount($UserID)
    {
        $UserID = ForceInt($UserID, 0);
        $TotalNumberOfRecords = 0;

        $db = Database::getInstance();
        $s = ObjectFactory::getInstance()->NewObject('SqlBuilder');
        $s->AddSelect('SearchID', 'us', 'Count', 'count');
        $s->SetMainTable('VanillaUserSearch', 'us');
        $s->AddWhere('us', 'UserID', '', $UserID, '=');

        $result = $db->Select($s, $this->Name, 'GetSearchCount', 'An error occurred while retrieving search summary data.');
        while ($rows = $db->GetRow($result)) {
            $TotalNumberOfRecords = $rows['Count'];
        }
        return $TotalNumberOfRecords;
    }

    function GetSearchList($RecordsToRetrieve = '0', $UserID)
    {
        $RecordsToRetrieve = ForceInt($RecordsToRetrieve, 0);

        $s = ObjectFactory::getInstance()->NewObject('SqlBuilder');
        $s = $this->GetSearchBuilder();
        $s->AddWhere('us', 'UserID', '', $UserID, '=');
        if ($RecordsToRetrieve > 0) {
            $s->AddLimit(0, $RecordsToRetrieve);
        }

        return Database::getInstance()->Select($s, $this->Name, 'GetSearchList', 'An error occurred while retrieving saved searches.');
    }

    function SaveSearch(&$Search)
    {
        // Validate the topic properties
        if ($Search->Label == '') {
            $this->Context->WarningCollector->Add($this->Context->GetDefinition('ErrSearchLabel'));
        }

        // If validation was successful, then reset the properties to db safe values for saving
        if ($this->Context->WarningCollector->Count() == 0) {
            $SearchToSave = $Search;
            $SearchToSave->FormatPropertiesForDatabaseInput();
            $s = ObjectFactory::getInstance()->NewObject('SqlBuilder');

            // Proceed with the save if there are no warnings
            if ($this->Context->WarningCollector->Count() == 0) {
                $s->SetMainTable('UserSearch',    'us');
                $s->AddFieldNameValue('Label',    $SearchToSave->Label);
                $s->AddFieldNameValue('UserID',   Session::getInstance()->UserID);
                $s->AddFieldNameValue('Type',     $SearchToSave->Type);
                $s->AddFieldNameValue('Keywords', $SearchToSave->Keywords);
                if ($SearchToSave->SearchID > 0) {
                    $s->AddWhere('us', 'SearchID', '', $SearchToSave->SearchID, '=');
                    Database::getInstance()->Update($s, $this->Name, 'SaveSearch', 'An error occurred while saving your search.');
                } else {
                    $Search->SearchID = Database::getInstance()->Insert($s, $this->Name, 'SaveSearch', 'An error occurred while creating your search.');
                }
            }

        }
        return $this->Context->WarningCollector->Iif();
    }
}